<?php
class Logo extends Podcast {

	public function generateOutput() {
		$query = "SELECT logo FROM `pp_podcasts` WHERE tag = '".self::$podcast."';";
		$res = self::$db->query(($query));
		
		header('Content-type: image/jpeg');

		if(mysql_num_rows($res)) {
			$logo = @mysql_fetch_array($res);
			echo @base64_decode($logo["logo"]);
		} else {
			readfile(ROOT."app/images/logo.png");
		}
	}
	
	public static function separateFromContent($feedContent) {
		$image = "";
		$default = false;

		// if there is a itunes image, use it
		$pattern = "#<itunes:image.*href=\"(.*)\".*>#Us";
		if(preg_match($pattern, $feedContent, $match)) {
			$image = @$match[1];
		} else {
			// otherwise look for a image tag
			$pattern = "#<image>(.*)</image>#Us";
			if(preg_match($pattern, $feedContent, $match)) {
				// separate url
				preg_match("#<url>(.*)</url>#U", $match[1], $match);
				$image = @$match[1];
			}
		}

		if($image == "") {
			$image = ROOT."app/images/logo.png";
			$default = true;
		}

		$si = new SimpleImage();
		$si->load($image);
		$si->resize("120", "120");
		$image = @$si->output();

		$image = chunk_split(base64_encode($image));
		
		$result["image"] = $image;
		$result["default"] = $default;

		return $result;
	}
}

?>
